import CONSTANT from '@/common/constants.js'
import http from '@/common/http.js'
import Group from 'vux/src/components/group/index.vue'
import XInput from 'vux/src/components/x-input/index.vue'
import XTextarea from 'vux/src/components/x-textarea/index.vue'
import CheckIcon from 'vux/src/components/check-icon/index.vue'
export default {
  name: 'volunteer',
  components: {Group, XInput, XTextarea, CheckIcon},
  data () {
    return {
      userId: '',
      imageHttp: 'http://www.qqchou.org/a/wxinterface/file/upload',
      sex2: false,
      sex1: false,
      political1: false,
      political2: false,
      political3: false,
      list: {
        volunteerLocal: '',
        volunteerTime: '',
        name: '',
        age: '',
        nation: '',
        idCardNum: '',
        local: '',
        campany: '',
        mobile: '',
        telphone: '',
        address: '',
        volunteerNo: '',
        volunteerTotalTime: '',
        talent: '',
        award: '',
        headUrl: '',
        sex: '',
        politicalLandscape: '',
        user_id: '',
        event_id: ''
      },
      sfzyanzhen: function (value) {
        let r = /^(\d{18}|\d{17}x)$/;
        if (r.test(value)) {
          return {
            valid: true,
            msg: '身份证正确'
          }
        } else {
          return {
            valid: false,
            msg: '身份证错误'
          }
        }
      }
    }
  },
  watch: {
    sex1: function (val) {
      console.log('nan', val)
      if (val == true) {
        this.sex2 = false
        this.list.sex = '男'
      } else {
        if (this.list.sex == '男') {
          this.list.sex = ''
        }
      }
    },
    sex2: function (val) {
      console.log('nv', val)
      if (val == true) {
        this.sex1 = false
        this.list.sex = '女'
      } else {
        if (this.list.sex == '女') {
          this.list.sex = ''
        }
      }
    },
    political1: function (val) {
      console.log('群众', val)
      if (val == true) {
        this.political2 = false
        this.political3 = false
        this.list.politicalLandscape = '群众'
      } else {
        if (this.list.politicalLandscape == '群众') {
          this.list.politicalLandscape = ''
        }
      }
    },
    political2: function (val) {
      console.log('团员', val)
      if (val == true) {
        this.political1 = false
        this.political3 = false
        this.list.politicalLandscape = '团员'
      } else {
        if (this.list.politicalLandscape == '团员') {
          this.list.politicalLandscape = ''
        }
      }
    },
    political3: function (val) {
      console.log('党员', val)
      if (val == true) {
        this.political1 = false
        this.political2 = false
        this.list.politicalLandscape = '党员'
      } else {
        if (this.list.politicalLandscape == '党员') {
          this.list.politicalLandscape = ''
        }
      }
    }
  },
  mounted () {
    if (this.$emit('ifOpenid')) {
      this.$emit('ifOpenid', this.$route.path)
    }
    this.userId = window.sessionStorage.getItem('userId')
    this.list.event_id = window.sessionStorage.getItem('event_id')
    this.event_id = this.list.event_id
    this.list.user_id = this.userId
    console.log(this.userId)
     http.post(CONSTANT.SEEK_VOLUNTEER, {user_id: this.userId, event_id: this.event_id}, (res) => {
       console.log(res)
       if (res.code == 200) {
         for (var key in res.obj) {
           for (var keyl in this.list) {
             if (key == keyl) {
               this.list[keyl] = res.obj[key]
             }
             if (key == 'sex' && keyl == 'sex') {
               if (res.obj[key] == '男') {
                 this.sex1 = true
               }
               if (res.obj[key] == '女') {
                 this.sex2 = true
               }
             }
             if (key == 'politicalLandscape' && keyl == 'politicalLandscape') {
               if (res.obj[key] == '团员') {
                 this.political2 = true
               } else if (res.obj[key] == '群众') {
                 this.political1 = true
               } else if (res.obj[key] == '党员') {
                 this.political3 = true
               }
             }
           }
         }
       }
     })
    this.list.volunteerLocal = this.$route.query.volunteerLocal
    this.list.volunteerTime = this.$route.query.volunteerTime
  },
  methods: {
    priment () {
      console.log(this)
      if (this.list.name == '') {
        alert('请输入您的姓名！')
      } else if (this.list.sex == '') {
        alert(' 请选择性别！')
      } else if (this.list.age == '') {
        alert('请输入年龄！')
      } else if (this.$refs.nl.valid == false) {
        alert('请输入正确的年龄')
      } else if (this.list.nation == '') {
        alert('请输入民族！')
      } else if (this.list.idCardNum == '') {
        alert('请输入身份证号码！')
      } else if (this.$refs.sfz.valid == false) {
        alert('请输入正确的身份证号码！')
      } else if (this.list.politicalLandscape == '') {
        alert('请选择政治面貌！')
      } else if (this.list.local == '') {
        alert('请输入您的籍贯！')
      } else if (this.list.campany == '') {
        alert('请输入您所在的单位！')
      } else if (this.list.mobile == '') {
        alert('请输入您的手机号！')
      } else if (this.$refs.sjh.valid == false) {
        alert('请输入正确的手机号')
      } else if (this.list.address == '') {
        alert('请输入您的具体地址！')
      } else if (this.list.headUrl == '') {
        alert('请上传您的个人照片！')
      } else {
        http.post(CONSTANT.VOLUNTEER, this.list, (res) => {
          console.log(res)
          if (res.code == 200) {
            alert(res.errmsg)
            window.history.go(-1)
          } else {
            alert('报名失败！')
            window.history.go(-1)
          }
        })
      }
    },
    handleAvatarSuccessz (res, file) {
      this.list.headUrl = res.url
    },
    beforeAvatarUpload (file) {
      var isJPEG = file.type === 'image/jpeg'
      var isPNG = file.type === 'image/png'
      var isGIF = file.type === 'image/gif'
      var isJPG = file.type === 'image/jpg'
      var isLt10M = file.size / 1024 / 1024 < 10
      if (!isJPEG && !isPNG && !isGIF && !isJPG) {
        alert('上传图片只能是 JPEG,PNG,GIF,JPG 格式!')
        // this.$refs.percentage.style.display = 'none'
        // this.$refs.upload.style.display = 'block'
        return false
      }
      if (!isLt10M) {
        alert('上传图片大小不能超过 10MB!')
        // this.$refs.percentage.style.display = 'none'
        // this.$refs.upload.style.display = 'block'
        return false
      }
    },
    handleAvatarError () {
      alert('上传头像失败！')
    },
    handleAvatarPorgress () {

    },
    comeBack () {
      window.history.back()
    }
  }
}
